package com.example.vue_shop.components.shoptype.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.vue_shop.components.shoptype.entity.ShopType;
import com.example.vue_shop.components.shoptype.service.ShopTypeService;
import com.example.vue_shop.utils.ResultN;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author lipengwei
 */
@Api(tags = "商品分类")
@RestController
@RequestMapping("/shopType")
public class ShopTypeController {

    @Resource(name = "shopTypeServiceImpl")
    private ShopTypeService shopTypeService;

    /**
     * 新增商品分类.
     *
     * @param shopType 商品分类对象
     * @return String
     */
    @ApiOperation("新增商品分类")
    @PostMapping("/addShopType")
    public String addShopType(@RequestBody ShopType shopType) {
        try {
            shopTypeService.addShopType(shopType);
            return ResultN.success("新增商品分类成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("新增商品分类失败！");
        }
    }

    /**
     * 获取商品分类列表，树形结构，含分页，不传分页则为获取全部商品分类.
     *
     * @param type     1，只差一级，2，查一级二级，3查三级
     * @param pageNum  页数
     * @param pageSize 每页显示的数量
     * @return String
     */
    @ApiOperation("获取商品分类列表(树形结构，含分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "查询类型，1 只查询一级商品分类，2查询1,"
                    + "2级商品分类，3 全部查询",defaultValue = "3"),
            @ApiImplicitParam(name = "pageNum", value = "页数"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示的数量")
    })
    @GetMapping("/getShopTypeList/tree")
    public String getShopTypeList(
            @RequestParam(required = false, defaultValue = "3") String type,
            @RequestParam(required = false) String pageNum,
            @RequestParam(required = false) String pageSize) {
        try {
            Map<String, Object> resultMap;
            if (StringUtils.isBlank(pageNum)) {
                resultMap = shopTypeService.getAllShopTypeList(type);
            } else {
                resultMap = shopTypeService
                        .getAllShopTypeListPage(type, Integer.parseInt(pageNum),
                                Integer.parseInt(pageSize));
            }
            return ResultN.success(resultMap, "获取商品分类列表成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("获取商品分类列表失败！");
        }
    }


    /**
     * 删除商品分类.
     *
     * @param id 商品分类Id
     * @return String
     */
    @ApiOperation("删除商品分类")
    @ApiImplicitParam(name = "id", value = "商品分类的Id", dataType = "int",
            example = "1", required = true)
    @DeleteMapping("/delShopType")
    public String delShopType(int id) {
        try {
            shopTypeService.delShopType(id);
            return ResultN.success("删除商品分类成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("删除商品分类失败！");
        }
    }

    /**
     * 更新商品分类.
     * @param shopType 商品分类实体类
     * @return String
     */
    @ApiOperation(value = "更新商品分类", notes = "更新商品分类")
    @PutMapping("/updateShopType")
    public String updateShopType(ShopType shopType) {
        try {
            shopTypeService.updateShopType(shopType);
            return ResultN.success("更新商品分类成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultN.error("更新商品分类失败！");
        }
    }

}
